var express = require('express');
var router = express.Router();
var sha1 = require('sha1');
var UserModel = require('../models/user');
var checkNotLogin = require('../middlewares/check').checkNotLogin;


router.post('/',function(req,res,next){
    let data = req.body;
    let name = data.userName;
    let pw = data.password;
    
    UserModel.getUserByName(name)
    .then(function (user){
        if (!user){
            
            return res.json({code:0,message:'用户不存在'});
        }
        if (pw !== user.password){

            return res.json({code:0,message:'用户密码错误'});
        } 
              // 用户信息写入 session
        // delete user.password;
        req.session.user = user;
        console.log( req.session);
        res.json({code:1, message: '登录成功'});
    })
    .catch((err)=>{
        console.log('err:' + err.message);
        res.json({code:0, message: err.message});
    });
});


module.exports = router;